package com.smooth.site.sx.pc.jifen.service;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.smooth.dao.gen.BizAddressMapper;
import com.smooth.dao.gen.bean.BaseAreas;
import com.smooth.dao.gen.bean.BaseCities;
import com.smooth.dao.gen.bean.BizAddress;
import com.smooth.dao.gen.bean.BizAddressExample;
import com.smooth.dao.gen.bean.BizStudent;
import com.smooth.site.sx.mag.address.service.BizAddressService;
import com.smooth.site.sx.mag.basedata.area.service.BaseAreasService;
import com.smooth.site.sx.mag.basedata.city.service.BaseCitiesService;
import com.smooth.site.sx.pc.jifen.vo.AddressReq;
import com.smooth.sys.pc.vo.ListRsp;

@Component
public class PcAdressService {
	@Autowired
	BizAddressMapper bizAddressMapper;
	@Autowired
	BizAddressService bizAddressService;
	public BizAddress queryAddressById(String id) {
		return bizAddressMapper.selectByPrimaryKey(id);
	}
	
	public ListRsp<BizAddress> queryAddressByTelNo(String telNo) {
		BizAddressExample bizAddressExample = new BizAddressExample();
		
		BizAddressExample.Criteria criteria = bizAddressExample.createCriteria();
		criteria.andUserTelnoEqualTo(telNo);
		
		List<BizAddress> blist = bizAddressMapper.selectByExample(bizAddressExample);
		
		ListRsp<BizAddress> listRsp = new ListRsp<BizAddress>();
		listRsp.setDatalist(blist);
		listRsp.setTsize(bizAddressMapper.countByExample(bizAddressExample));
		return listRsp;
		
	}
	@Autowired
	BaseAreasService baseAreasService;
	@Autowired
	BaseCitiesService baseCitiesService;
	public BizAddress saveBizAddress(AddressReq addressReq,BizStudent bizStudent) {
		BizAddress bizAddress = new BizAddress();
		bizAddress.setAddressDetail(addressReq.getAddressDetail());
		bizAddress.setAreaId(addressReq.getAreaId());
		bizAddress.setId(addressReq.getAddressId());
		bizAddress.setUserId(bizStudent.getId());
		if(StringUtils.isBlank(addressReq.getShouhuoName())) {
			bizAddress.setUserName(bizStudent.getStuName());
		}else {
			bizAddress.setUserName(addressReq.getShouhuoName());
		}
		bizAddress.setUserTelno(addressReq.getTelNo());
		setAddressInfo(addressReq,bizAddress);
		BizAddress bizAddressDb = bizAddressService.save(bizAddress);
		return bizAddressDb;
	}
	
	private void setAddressInfo(AddressReq addressReq,BizAddress bizAddress) {
		BaseAreas baseAreas = baseAreasService.get(addressReq.getAreaId());
		bizAddress.setAreaName(baseAreas.getArea());
		
		BaseCities baseCities = baseCitiesService.get(baseAreas.getCityId());
		bizAddress.setCityId(baseCities.getId());
		bizAddress.setCityName(baseCities.getCityName());
		bizAddress.setProvinceId(baseCities.getProvinceId());
		bizAddress.setProvinceName(baseCities.getProvinceName());
	}
	public BizAddress updBizAddress(BizAddress bizAddress) {
		return bizAddressService.save(bizAddress);
	}
	
}
